# 不使用 CrawlSpider 进行爬取
import scrapy

from Midde.items import MiddeItem


class ImgpictureSpider(scrapy.Spider):
    name = 'imgpicture'
    start_urls = ["https://news.daxues.cn/xiaohua/ziliao/"]
    url = 'https://news.daxues.cn/xiaohua/ziliao/index_%s.html'
    page_num = 1

    def parse(self, response, **kwargs):
        print(
            f"""The target URL information is crawled successfully, the URL is{response.url}, and the return status code is {response.status}""")
        img_list = response.xpath("""/html/body/div[2]/div[2]/dl""")

        for li in img_list:
            img_name = li.xpath("./dt/a/text()").extract_first() + ".jpg"
            img_picture = "https://news.daxues.cn/" + li.xpath("./a/img/@src").extract_first()

            items = MiddeItem()
            items["name"] = img_name
            items["src"] = img_picture

            yield items

        if response.status == 200:
            self.page_num += 1
            new_url = self.url % self.page_num
            yield scrapy.Request(url=new_url, callback=self.parse)
